package com.uroad.lib.widget.photopicker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uroad.lib.R;
import com.uroad.lib.widget.photopicker.fragment.ImagePagerFragment;
import com.uroad.lib.widget.photopicker.widget.MultiPickResultView;
import com.uroad.lib.widget.photopicker.widget.Titlebar;

import java.util.List;

import static com.uroad.lib.widget.photopicker.PhotoPicker.KEY_SELECTED_PHOTOS;
import static com.uroad.lib.widget.photopicker.PhotoPreview.EXTRA_ACTION;
import static com.uroad.lib.widget.photopicker.PhotoPreview.EXTRA_CURRENT_ITEM;
import static com.uroad.lib.widget.photopicker.PhotoPreview.EXTRA_PHOTOS;
import static com.uroad.lib.widget.photopicker.PhotoPreview.EXTRA_SHOW_DELETE;


public class PhotoPagerActivity extends AppCompatActivity {

    private ImagePagerFragment pagerFragment;

    private boolean showDelete;
    private Titlebar titlebar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.picker_activity_photo_pager);

        int currentItem = getIntent().getIntExtra(EXTRA_CURRENT_ITEM, 0);
        List<String> paths = getIntent().getStringArrayListExtra(EXTRA_PHOTOS);
        showDelete = getIntent().getBooleanExtra(EXTRA_SHOW_DELETE, true);
        int action = getIntent().getIntExtra(EXTRA_ACTION, MultiPickResultView.ACTION_ONLY_SHOW);

        if (pagerFragment == null) {
            pagerFragment =
                    (ImagePagerFragment) getSupportFragmentManager().findFragmentById(R.id.photoPagerFragment);
        }
        pagerFragment.setPhotos(paths, currentItem);
        titlebar = (Titlebar) findViewById(R.id.titlebar);
        titlebar.init(this);
        if (action == MultiPickResultView.ACTION_SELECT) {
            titlebar.setRitht(getApplicationContext().getResources().getDrawable(R.drawable.selector_picker_delete), "", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = pagerFragment.getViewPager().getCurrentItem();
                    if (pagerFragment.getPaths().size() > 0) {
                        pagerFragment.getPaths().remove(position);
                        pagerFragment.getViewPager().getAdapter().notifyDataSetChanged();
                        if (pagerFragment.getPaths().size() == 0) {
                            titlebar.setTitle(getString(R.string.__picker_preview) + " " + getString(R.string.__picker_image_index, 0,
                                    pagerFragment.getPaths().size()));
                            Intent intent = new Intent();
                            intent.putExtra(KEY_SELECTED_PHOTOS, pagerFragment.getPaths());
                            setResult(RESULT_OK, intent);
//                            finish();
                        }

                    }
                }
            });
            titlebar.setLeftOnclickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onBackPressed();
                }
            });
        }
        titlebar.setTitle(getString(R.string.__picker_preview));

        pagerFragment.getViewPager().addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                titlebar.setTitle(getString(R.string.__picker_preview) + " " + getString(R.string.__picker_image_index, pagerFragment.getViewPager().getCurrentItem() + 1,
                        pagerFragment.getPaths().size()));
            }
        });
    }

    //把actionBar的文字标题居中
    public static void centerActionBarTitle(Activity activity) {
        int titleId = activity.getResources().getIdentifier("action_bar_title", "id", "android");
        if (titleId <= 0) return;
        TextView titleTextView = (TextView) activity.findViewById(titleId);
        DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
        LinearLayout.LayoutParams txvPars = (LinearLayout.LayoutParams) titleTextView.getLayoutParams();
        txvPars.gravity = Gravity.CENTER_HORIZONTAL;
        txvPars.width = metrics.widthPixels;
        titleTextView.setLayoutParams(txvPars);
        titleTextView.setGravity(Gravity.CENTER);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (showDelete) {
            getMenuInflater().inflate(R.menu.__picker_menu_preview, menu);
        }
        return true;
    }


    @Override
    public void onBackPressed() {

        Intent intent = new Intent();
        intent.putExtra(KEY_SELECTED_PHOTOS, pagerFragment.getPaths());
        setResult(RESULT_OK, intent);
        finish();
        super.onBackPressed();
    }
}
